package cn.mifengkong.appcommon.webview.jshandler;

import android.content.Context;
import android.view.View;

import com.mifengkong.frtools.jsbridge.CallBackFunction;
import com.mifengkong.frtools.jsbridge.JsHandler;
import com.mifengkong.frwidget.titlebar.WebViewTitleBarUIComponent;

import org.json.JSONException;
import org.json.JSONObject;

import cn.mifengkong.appcommon.util.DeepLinkForNatvieUrlUtil;
import cn.mifengkong.appcommon.webview.InfoToJsonUtil;
import cn.mifengkong.appcommon.webview.listener.JsBridgeCallBackListener;
import cn.mifengkong.appcommon.webview.uicomponent.WebViewUIComponent;

/**
 * @author jiangyongxing
 * @date 2017/10/9
 * 描述：往title上面部分功能
 */

public abstract class CustomeWebviewTitleHandler implements JsHandler {

    protected static final String HTTP = "http";
    protected final Context mContext;
    protected WebViewUIComponent mWebViewUIComponent;
    protected WebViewTitleBarUIComponent mWebViewTitleBarUIComponent;
    protected String mApplicationId;
    private final JsBridgeCallBackListener jsBridgeCallBackListener;

    public CustomeWebviewTitleHandler(Context context, WebViewUIComponent webViewUIComponent,
                                      WebViewTitleBarUIComponent titleBarUIComponent, String applicationId, JsBridgeCallBackListener jsBridgeCallBackListener) {
        this.mContext = context;
        this.mWebViewUIComponent = webViewUIComponent;
        this.mWebViewTitleBarUIComponent = titleBarUIComponent;
        this.mApplicationId = applicationId;
        this.jsBridgeCallBackListener = jsBridgeCallBackListener;
    }

    protected void parseResponseData(String responseData, CallBackFunction function) {
        try {
            JSONObject jsonObject = new JSONObject(responseData);
            String iconUrl = jsonObject.getString("iconUrl");
            String jumpUrl = jsonObject.getString("url");
            String text = jsonObject.getString("text");
            IvOnclickListener ivOnclickListener = new IvOnclickListener(mContext, mWebViewUIComponent, jumpUrl, mApplicationId, function, jsBridgeCallBackListener);
            setImageToView(iconUrl,jumpUrl,text,ivOnclickListener);
        } catch (JSONException e) {
            InfoToJsonUtil.assemblyIntoFailureJson(function, new JSONObject());
            e.printStackTrace();

        }
    }

    protected abstract void setImageToView(String iconUrl, String jumpUrl, String text, IvOnclickListener ivOnclickListener);


    public static class IvOnclickListener implements View.OnClickListener {
        private final WebViewUIComponent webViewUIComponent;
        private final String mApplicationId;
        private final Context mContext;
        private final JsBridgeCallBackListener mJsBridgeCallBackListener;
        private final String mNativeUrl;
        private final CallBackFunction function;
        private String[] mNameAndParamters;

        public IvOnclickListener(Context context, WebViewUIComponent webViewUIComponent, String nativeUrl, String applicationId, CallBackFunction function, JsBridgeCallBackListener jsBridgeCallBackListener) {
            this.mContext = context;
            this.webViewUIComponent = webViewUIComponent;
            this.mNativeUrl = nativeUrl;
            this.mApplicationId = applicationId;
            this.function = function;
            this.mJsBridgeCallBackListener = jsBridgeCallBackListener;
        }

        @Override
        public void onClick(View v) {
            if (mNativeUrl.startsWith(HTTP)) {
                webViewUIComponent.loadUrlAndData(mNativeUrl);
            } else {
                mNameAndParamters = DeepLinkForNatvieUrlUtil.splitNativeUrl(mNativeUrl);
                DeepLinkForNatvieUrlUtil.startActivityOrFunction(mContext, mNameAndParamters,
                        DeepLinkForNatvieUrlUtil.nativeUrlToActivityOrFunction(mNativeUrl), mApplicationId,
                        function, mJsBridgeCallBackListener);
            }
        }
    }


}
